/*
 * This code is property of CoreObjects. Use, duplication and disclosure
 * in any form without permission of copyright holder is prohibited.
 *
 */
package com.nen.session;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Handles Session events - creation, destruction/invalidation.
 * Also listens to session attribute events - add/replace/remove
 * keeps the SessionMapHolder updated as per the loginUserDetails attribute lifecycle
 *
 * @author amitm
 */
public class JavaSessionListener implements HttpSessionListener, HttpSessionAttributeListener {
    private static final String ATTRIBUTE_NAME_LOGINUSERDETAILS = "loginUserDetails";

    private int sessionCount = 0;

    public void sessionCreated(HttpSessionEvent event) {
        synchronized (this) {
            sessionCount++;
        }

        System.out.println("Session Created: " + event.getSession().getId());
        System.out.println("Attribute value = "+event.getSession().getAttribute(ATTRIBUTE_NAME_LOGINUSERDETAILS));
        System.out.println("Total Sessions: " + sessionCount);
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        synchronized (this) {
            sessionCount--;
        }
        System.out.println("Session Destroyed: " + event.getSession().getId());
        System.out.println("Total Sessions: " + sessionCount);
    }

    public void attributeAdded(HttpSessionBindingEvent hsbe) {
        System.out.println("Added AttributeName="+hsbe.getName());
        System.out.println("Added AttributeValue="+hsbe.getValue());
        if(ATTRIBUTE_NAME_LOGINUSERDETAILS.equals(hsbe.getName())) {
            System.out.println("Login attribute added.");
            SessionMapHolder.addSession(hsbe.getSession().getId(), hsbe.getValue());
            System.out.println("SessionMapHolder populated");
        }
        
    }

    public void attributeRemoved(HttpSessionBindingEvent hsbe) {
        System.out.println("Removed AttributeName="+hsbe.getName());
        System.out.println("Removed AttributeValue="+hsbe.getValue());
        if(ATTRIBUTE_NAME_LOGINUSERDETAILS.equals(hsbe.getName())) {
            System.out.println("Login attribute removed.");
            SessionMapHolder.invalidateSession(hsbe.getSession().getId());
            System.out.println("Removed session from SessionMapHolder");
        }
    }

    public void attributeReplaced(HttpSessionBindingEvent hsbe) {
        System.out.println("Replaced AttributeName="+hsbe.getName());
        System.out.println("Replaced AttributeValue="+hsbe.getValue());
        if(ATTRIBUTE_NAME_LOGINUSERDETAILS.equals(hsbe.getName())) {
            System.out.println("Login attribute replaced.");
            SessionMapHolder.addSession(hsbe.getSession().getId(), hsbe.getValue());
            System.out.println("SessionMapHolder updated");
        }
    }
}

